package cn.cmkj.auction.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.cmkj.auction.R;

/**
 * Created by cunguoyao on 2018/4/19.
 */
@ContentView(R.layout.fragment_main_paimai)
public class MainPaimaiFragment extends BaseFragment implements View.OnClickListener {

    private View view;
    private FragmentManager fragmentManager;
    private Fragment currentFragment;
    private PaimaiListFragment paimaiListFragment1;
    private PaimaiListFragment paimaiListFragment2;
    private PaimaiListFragment paimaiListFragment3;

    @ViewInject(R.id.radio_today)
    private LinearLayout radio1;
    @ViewInject(R.id.radio_tomorrow)
    private LinearLayout radio2;
    @ViewInject(R.id.radio_yesterday)
    private LinearLayout radio3;

    @ViewInject(R.id.radio_today_text)
    private TextView radioTodayText;
    @ViewInject(R.id.radio_tomorrow_text)
    private TextView radioTomorrowText;
    @ViewInject(R.id.radio_yesterday_text)
    private TextView radioYesterdayText;

    public static MainPaimaiFragment create() {
        MainPaimaiFragment f = new MainPaimaiFragment();
        Bundle args = new Bundle();
        //args.putInt("type", type);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null) {
            view = x.view().inject(this, inflater, container);
        }
        fragmentManager = getChildFragmentManager();
        radio1.setOnClickListener(this);
        radio2.setOnClickListener(this);
        radio3.setOnClickListener(this);
        radio1.performClick();
        return view;
    }

    private void changeFragment(int btnId) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        switch (btnId) {
            case R.id.radio_today:
                if (paimaiListFragment1 == null) {
                    paimaiListFragment1 = PaimaiListFragment.create(1);
                    transaction.add(R.id.content, paimaiListFragment1, PaimaiListFragment.class.getSimpleName() + "_1");
                }
                currentFragment = paimaiListFragment1;
                transaction.show(paimaiListFragment1);
                break;
            case R.id.radio_tomorrow:
                if (paimaiListFragment2 == null) {
                    paimaiListFragment2 = PaimaiListFragment.create(2);
                    transaction.add(R.id.content, paimaiListFragment2, PaimaiListFragment.class.getSimpleName() + "_2");
                }
                currentFragment = paimaiListFragment2;
                transaction.show(paimaiListFragment2);
                break;
            case R.id.radio_yesterday:
                if (paimaiListFragment3 == null) {
                    paimaiListFragment3 = PaimaiListFragment.create(3);
                    transaction.add(R.id.content, paimaiListFragment3, PaimaiListFragment.class.getSimpleName() + "_3");
                }
                currentFragment = paimaiListFragment3;
                transaction.show(paimaiListFragment3);
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radio_today:
                radio1.setSelected(true);
                radio2.setSelected(false);
                radio3.setSelected(false);
                changeFragment(R.id.radio_today);
                break;
            case R.id.radio_tomorrow:
                radio1.setSelected(false);
                radio2.setSelected(true);
                radio3.setSelected(false);
                changeFragment(R.id.radio_tomorrow);
                break;
            case R.id.radio_yesterday:
                radio1.setSelected(false);
                radio2.setSelected(false);
                radio3.setSelected(true);
                changeFragment(R.id.radio_yesterday);
                break;
        }
    }

    public void setRadioText(int type, int count) {
        if(type == 1) {
            radioTodayText.setText(count + "件");
        }else if(type == 2) {
            radioTomorrowText.setText(count + "件");
        }else if(type == 3) {
            radioYesterdayText.setText(count + "件");
        }
    }
}
